< previous page page_230 next page >

Page 230
Note that this actually creates a 21-byte array with bytes numbered from 0 to 20. Next, assign a pointer to the array obtained using the VarPtr operator as shown here:
ul2.usri2_logon_hours = VarPtr(HourList(0))
All of the bytes in the array are stored in a contiguous memory block, so a pointer to the first byte in the array is the same thing as a pointer to the array.
Array Contents
The one thing I haven't discussed yet is what you would store in this array. Although not part of the original question, it is worth looking at because it illustrates some very interesting techniques that are not familiar to many Visual Basic programmers.
The array contents are defined as follows in the Win32 SDK reference:
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
Points to a 21-byte (168 bits) bit string that specifies the times during which the user can log on. Each bit represents a unique hour in the week. The first bit (bit 0, word 0) is Sunday, 0:00 to 0:59; the second bit (bit 1, word 0) is Sunday, 1:00 to 1:59; and so on. A null pointer in this element for NetUserAdd calls means that there is no time restriction. A null pointer in this element for NetUserSetInfo calls means that no change is to be made.
This is a rather interesting way of specifying times. It makes sense when you think about it. If you have 24 hours in a day and 7 days a week (as is common in most locations outside of Silicon Valley, where we routinely add hours to each day in order to have enough time to do things like writing these books), you need a total of 24 x 7 = 168 bits to allow for one bit for each hour.
How would you go about setting or clearing individual bits in the array?
Let's say, for this example, that days will be numbered 1 to 7 (Sunday through Saturday) and hours from 0 to 23. (This system uses 24-hour time. For those of you unfamiliar with this time format, all you need to know is that 13 o'clock is 1:00 PM and that you add 12 to convert after that.)
We need to convert the day and hour to an index into an array and a bit location within the indexed byte.
Fortunately, in this case, the numbers are fairly easy to calculate. Each byte holds 8 bits for 8 hours. Three bytes represents a day. We can use the following equation to figure out which byte to use:
ByteIndex = Int(Hour / 8) + ((Day - 1) * 3)

 
< previous page page_230 next page >